<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>歌单列表</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">
    <style>
        #userInfoDialog {
            padding: 20px;
            width: 400px;
            display: none; /*隐藏层*/
        }

        #userInfoDialog p {
            padding: 10px 0;
        }
        
        .layui-table-tool-temp {
            padding-right: 120px !important;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">歌单列表</div>
        <div class="layui-card-body">
            <table id="musiclist" lay-filter="musiclistFilter"></table>
        </div>
    </div>
</div>

<!--歌单详情对话框-->
<div id="details" style="display: none;padding: 20px">
    <table class="layui-table" lay-skin="nob">
        <tr>
            <td rowspan="3">
                <img src="" id="coverimg" height="96px" alt="">
            </td>
            <td colspan="2">
                <h2><span id="listname"></span></h2>
                <span><i class="layui-icon layui-icon-username"></i><span id="create"></span></span>
            </td>
        </tr>
        <tr>
            <td colspan="2">标签: <span id="tags"></span></td>
        </tr>
        <tr>
            <td>点击量: <span id="clickcount"></span></td>
            <td>歌曲总数: <span id="musiccount"></span></td>
        </tr>
    </table>

    <!-- 专辑详情表-->
    <table id="musiclistDetails"></table>
</div>

<!-- 添加/编辑歌单对话框 -->
<div id="editDialog" style="display: none; padding: 20px;">
    <form class="layui-form" action="" lay-filter="musicListForm">
        <input type="hidden" name="id" />
        <div class="layui-form-item">
            <label class="layui-form-label">歌单名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入歌单名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">封面图片</label>
            <div class="layui-input-block">
                <input type="text" name="pic" placeholder="请输入封面图片URL" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="上架" checked>
                <input type="radio" name="status" value="0" title="下架">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="saveBtn">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<script src="../res/layui/layui.js"></script>
<script>
    layui.use(function () {
        //加载获取table模块
        var table = layui.table;
        //加载获取layer弹层模块
        var layer = layui.layer;
        //加载jquery对象
        var $ = layui.jquery;
        // 加载form模块
        var form = layui.form;
        
        // 渲染表格
        table.render({
            elem: '#musiclist',
            url: '/musicList/list', // 数据接口
            page: true, // 开启分页
            limit: 5,
            limits: [5, 10, 15, 20],
            cols: [[
                {field: 'id', title: 'ID', width: 80, sort: true},
                {field: 'name', title: '歌单名称', width: 200},
                {field: 'pic', title: '封面图片', width: 150, templet: function(d){
                    if(d.pic) {
                        return '<img src="'+d.pic+'" height="30">';
                    }
                    return '';
                }},
                {field: 'status', title: '状态', width: 100, templet: function(d){
                    return d.status === '1' ? '<span style="color: green;">上架</span>' : '<span style="color: red;">下架</span>';
                }},
                {field: 'createTime', title: '创建时间', width: 180, templet: function(d){
                    return layui.util.toDateString(d.createTime);
                }},
                {field: 'uid', title: '用户ID', width: 100},
                {title: '操作', toolbar: '#rowTools', width: 180}
            ]],
            toolbar: '#toolbarDemo'
        });
        
        // 表格头部工具栏
        var toolbarDemo = '<div>' +
            '<button class="layui-btn layui-btn-sm" lay-event="add">添加歌单</button>' +
            '</div>';
            
        $('#musiclist').before('<script id="toolbarDemo" type="text/html">'+toolbarDemo+'<\/script>');
        
        // 头部工具栏事件
        table.on('toolbar(musiclistFilter)', function(obj){
            switch(obj.event){
                case 'add':
                    openEditDialog();
                break;
            }
        });
        
        // 监听行工具事件
        table.on('tool(musiclistFilter)', function(obj){
            var data = obj.data; // 获得当前行数据
            switch(obj.event){
                case 'details':
                    showDetails(data);
                break;
                case 'edit':
                    openEditDialog(data);
                break;
                case 'del':
                    deleteMusicList(data.id, obj);
                break;
            }
        });
        
        // 打开编辑对话框
        function openEditDialog(data) {
            // 清空表单
            form.val("musicListForm", {
                "id": "",
                "name": "",
                "pic": "",
                "status": "1"
            });
            
            if(data) {
                // 编辑模式，填充表单数据
                form.val("musicListForm", data);
            }
            
            layer.open({
                type: 1,
                title: data ? '编辑歌单' : '添加歌单',
                content: $('#editDialog'),
                area: ['500px', '400px'],
                success: function(layero, index){
                    // 重新渲染表单
                    form.render();
                }
            });
        }
        
        // 显示详情
        function showDetails(data) {
            layer.open({
                type: 1,
                title: '歌单详情',
                content: $('#details'),
                area: ['600px', '400px']
            });
        }
        
        // 删除歌单
        function deleteMusicList(id, obj) {
            layer.confirm('确定要删除这个歌单吗？', function(index){
                $.ajax({
                    url: '/musicList/delete?id=' + id,
                    type: 'DELETE',
                    success: function(result) {
                        if(result.code === 0) {
                            layer.msg('删除成功');
                            obj.del(); // 删除对应行
                        } else {
                            layer.msg('删除失败：' + result.msg);
                        }
                    },
                    error: function() {
                        layer.msg('请求失败');
                    }
                });
                layer.close(index);
            });
        }
        
        // 监听提交
        form.on('submit(saveBtn)', function(data){
            var url = data.field.id ? '/musicList/update' : '/musicList/add';
            var type = data.field.id ? 'PUT' : 'POST';
            
            $.ajax({
                url: url,
                type: type,
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function(result) {
                    if(result.code === 0) {
                        layer.msg(data.field.id ? '更新成功' : '添加成功');
                        layer.closeAll(); // 关闭所有弹窗
                        table.reload('musiclist'); // 重新加载表格
                    } else {
                        layer.msg(data.field.id ? '更新失败：' + result.msg : '添加失败：' + result.msg);
                    }
                },
                error: function() {
                    layer.msg('请求失败');
                }
            });
            
            return false; // 阻止表单跳转
        });
    });
</script>

<!--操作栏自定义模板-->
<script id="rowTools" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="details">
            <i class="layui-icon layui-icon-more"></i>
            详情
        </button>
        <button class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
            修改
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </div>
</script>

</body>
</html>